package com.sec.android.app.myfiles.decorator.dlpinfo;

import android.content.Context;
import android.view.View;
import android.view.ViewStub;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.decorator.AbsAdapterImpDecorator;
import com.sec.android.app.myfiles.feature.DlpMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

/**
 * Created by jaebae on 2016-04-22.
 */
public class DlpAdapterDecorator extends AbsAdapterImpDecorator {
    private AbsMyFilesFragment mFragment;

    public DlpAdapterDecorator(Context context, AbsMyFilesFragment fragment, FileListAdapter adapter) {
        super(context, adapter);
        mFragment = fragment;
    }

    @Override
    protected void _bindCreateFolderView(ListViewHolder vh, View v) {
        // do nothing
    }

    @Override
    protected void _createViewHolder(View v, ListViewHolder vh) {
        vh.addView(FileListViewHolder.DLP_ICON_STUB, v.findViewById(R.id.dlp_icon_stub));
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        _bindDLPIcon(context, vh, v, record);
    }

    @Override
    protected void _bindProgressSync(ListViewHolder vh, View v, FileRecord record, boolean isShow) {
        // do nothing
    }

    public static void showDLPIcon(ListViewHolder vh, View v, boolean show) {
        View dlpIcon = vh.getView(FileListViewHolder.DLP_ICON);
        if (show && dlpIcon == null) {
            dlpIcon = ensureDLPIcon(vh, v);
        }
        if (dlpIcon != null) {
            dlpIcon.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    private static View ensureDLPIcon(ListViewHolder vh, View v) {
        View ret;
        ViewStub stub = vh.getView(FileListViewHolder.DLP_ICON_STUB, ViewStub.class);
        if (stub != null) {
            ret = stub.inflate();
        } else {
            ret = v.findViewById(R.id.dlp_icon);
        }

        if (ret != null) {
            vh.addView(FileListViewHolder.DLP_ICON, ret);
        }
        return ret;
    }

    protected void _bindDLPIcon(Context context, ListViewHolder vh, View v, final FileRecord record) {
        int endPadding = 0;
        int startPadding = 0;
        boolean isDLP = DlpMgr.getInstance(mContext).isDlpFile(record);
        NavigationInfo navigationInfo = NavigationManager.getInstance(mFragment.getProcessId()).getCurInfo();

        showDLPIcon(vh, v, isDLP);
        if (!mAdapterImp.isGridList()) {
            if (isDLP) {
                endPadding = context.getResources().getDimensionPixelSize(R.dimen.list_item_text_private_content_padding_end);
            }

            if (navigationInfo != null && navigationInfo.getNavigationMode() == NavigationInfo.NavigationMode.Pick_files) {
                if (record.isAudioFileType()) {
                    endPadding = context.getResources().getDimensionPixelSize(R.dimen.list_item_text_private_pickerplay_padding) + endPadding;
                }
            }
            View name = vh.getView(FileListViewHolder.NAME);
            if (name != null) {
                name.setPaddingRelative(name.getPaddingStart(), name.getPaddingTop(), endPadding, name.getPaddingBottom());
            }

            View date = vh.getView(FileListViewHolder.DATE);
            if (date != null) {
                date.setPaddingRelative(date.getPaddingStart(), date.getPaddingTop(), endPadding, date.getPaddingBottom());
            }
        }
    }
}
